package com.kingtop.base.service.impl;

import com.kingtop.base.dao.IBaseRoleDao;
import com.kingtop.base.dao.IBaseUserDao;
import com.kingtop.base.dao.IBaseUserRoleDao;
import com.kingtop.base.model.BaseRole;
import com.kingtop.base.model.BaseUser;
import com.kingtop.base.model.BaseUserRole;
import com.kingtop.base.service.IBaseRoleService;
import org.kingtop.mybatis.mapper.BaseServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("BaseRoleService")
public class BaseRoleServiceImpl extends BaseServiceImpl<BaseRole> implements IBaseRoleService {

	@Resource
	private IBaseRoleDao baseRoleDao;

	@Resource
	private IBaseUserDao baseUserDao;
	
	@Resource
	private IBaseUserRoleDao baseUserRoleDao;

	@Override
	public Set<String> listRolesByName(String userName) {
		if (userName != null && !"".equals(userName)) {
			BaseUser user = this.baseUserDao.getByUsername(userName);
			if (user != null) {
				List<BaseRole> list = this.baseRoleDao.listByUserId(user.getId());
				Set<String> roles = new HashSet<String>();
				for (BaseRole role : list) {
					roles.add(role.getRoleSign());
				}
				return roles;
			}
		}
		return new HashSet<String>();
	}

	@Override
	public BaseRole getById(long id) {
		return this.baseRoleDao.getById(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseUserRole> listUserRoleByUserId(long userId) {
		return this.baseUserRoleDao.listUserRoleByUserId(userId);
	}

}